package web;

import java.io.IOException;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import dao.*;
import entity.*;

public class PruefungBearbeitenServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		long pruefungDbID = Long.parseLong(req.getParameter("pruefungDbID"));
		Pruefung pruefung = new PruefungDAO().load(pruefungDbID);
		req.setAttribute("pruefung", pruefung);

		List<Lva> lvaEntries = new LvaDAO().loadAll();
		req.setAttribute("lvaEntries", lvaEntries);

		req.getRequestDispatcher("PruefungBearbeiten.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		try {
			long pruefungDbID = Long
					.parseLong(req.getParameter("pruefungDbID"));

			Pruefung pruefung = new PruefungDAO().load(pruefungDbID);

			if (pruefung == null) {
				pruefung = new Pruefung();
			}

			String datum = req.getParameter("datum");
			pruefung.setDatum(datum);

			String uhrzeit = req.getParameter("uhrzeit");
			pruefung.setUhrzeit(uhrzeit);

			Double dauer = Double.parseDouble(req.getParameter("dauer"));
			pruefung.setDauer(dauer);

			String id = req.getParameter("id");
			pruefung.setId(id);

			long lvaDbID = Long.parseLong(req.getParameter("lva"));
			Lva lva = new LvaDAO().load(lvaDbID);
			pruefung.setLva(lva);

			new PruefungDAO().update(pruefung);
			resp.sendRedirect("LVAIndex");
		} catch (Exception e) {
			e.printStackTrace();
			resp.sendRedirect("ErrorPage");
		}

	}
}
